<template>
  <div id="BasicMaterial"></div>
</template>

<script setup lang="ts">
import { onMounted } from 'vue'
import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'

const scene = new THREE.Scene()
const geometry = new THREE.TorusKnotGeometry(3, 1, 50, 8)
const solideMaterial = new THREE.MeshBasicMaterial({
    color: '#2b7098',
    transparent: true,
    opacity: 0.8
})
/**
 * basicMaterial
 * 基础材质： MeshBasicMaterial，这种材质不受光照影响，只显示颜色
 * color: 颜色
 * wireframe: 是否显示线框
 * vertexColors: 是否使用顶点颜色
 * transparent: 是否透明
 * opacity: 透明度
 */
const humbelMaterial = new THREE.MeshBasicMaterial({
    color: 'white',
    wireframe: true,
    vertexColors: true,
})
const solideMesh = new THREE.Mesh(geometry, solideMaterial)
const humbelMesh = new THREE.Mesh(geometry, humbelMaterial)
scene.add(solideMesh)
scene.add(humbelMesh)

const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000)
camera.position.z = 30

/**
 * antialias: 是否开启抗锯齿,开启后画面会更加平滑
 * alpha: 是否开启透明度,开启后可以设置背景色透明
 * setSize: 设置渲染器的大小
 * setClearColor: 设置背景颜色;第一个参数是颜色，第二个参数是透明度
 * setPixelRatio: 设置设备像素比;默认是1，设置为2的时候，画面会变得更加清晰
 */
const renderer = new THREE.WebGLRenderer({
    antialias: true,
    alpha: true,
})
renderer.setClearColor(0x444442, 0.6); //设置背景颜色
renderer.setSize(window.innerWidth, window.innerHeight)
renderer.setPixelRatio(window.devicePixelRatio)

const animate = () => {
    requestAnimationFrame(animate)
    solideMesh.rotation.x += 0.001
    solideMesh.rotation.y += 0.001
    solideMesh.rotation.z += 0.001
    humbelMesh.rotation.x += 0.001
    humbelMesh.rotation.y += 0.001
    humbelMesh.rotation.z += 0.001
    renderer.render(scene, camera)
}
onMounted(() => {
    console.log('查看当前屏幕设备像素比', window.devicePixelRatio)
    new OrbitControls(camera, renderer.domElement)
    document.getElementById('BasicMaterial')?.appendChild(renderer.domElement)
    animate()
})
window.onresize = () => {
    renderer.setSize(window.innerWidth, window.innerHeight)
    camera.aspect = window.innerWidth / window.innerHeight
    camera.updateProjectionMatrix()
}
</script>

<style>

</style>